//
//  APIManager.swift
//  TodayNews
//
//  Created by ZhangBo on 2019/6/17.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import Foundation
import Moya

enum APIManager {
    case newsTitles([String: Any])
    case searchSuggestInfo([String: Any])
    case newsFeeds([String: Any])
    case moreNewsFeeds([String: Any])
}

extension APIManager: TargetType {
    //基础URL
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }

    //RUL 路径
    var path: String {
        switch self {
        case .newsTitles:
            return "/article/category/get_subscribed/v1/?"
        case .searchSuggestInfo:
            return "/search/suggest/homepage_suggest/?"
        case .newsFeeds:
            return "/api/news/feed/v75/?"
        case .moreNewsFeeds:
            return "/api/news/feed/v75/?"
        }
    }

    //请求类型
    var method: Moya.Method {
        switch self {
        case .newsTitles:
            return .get
        case .newsTitles, .searchSuggestInfo, .newsFeeds, .moreNewsFeeds:
            return .post
        }
    }

    //请求参数类型
    public var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }

    //请求任务
    var task: Task {
        switch self {
        case .newsTitles(let params),
             .searchSuggestInfo(let params),
             .newsFeeds(let params),
             .moreNewsFeeds(let params):
            d_print(params)
            return .requestParameters(parameters: params, encoding: JSONEncoding.default)
        }
    }

    //请求header添加参数
    var headers: [String: String]? {
        return ["Content-type": "application/json"]
    }

    //单元测试
    var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }
}
